package caiziyou.com.designpattern.proxy.cglibproxy;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class ProxyFactory implements MethodInterceptor{

	//维护一个目标对象
	private Object target;

	//构造器，传入一个被代理的对象
	public ProxyFactory(Object target) {
		this.target = target;
	}
	
	//返回一个代理对象：是target对象的代理对象
	public Object getProxyInstance() {
		//创建一个工具类
		Enhancer enhancer = new Enhancer();
		//设置父类
		enhancer.setSuperclass(target.getClass());
		//设置回调的值
		enhancer.setCallback(this);
		//创建子类对象，即代理对象
		return enhancer.create();
	}

	@Override
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		System.out.println("cglib 代理开始");
		Object returnVal = method.invoke(target, args);
		System.out.println("cglib 代理结束");
		return returnVal;
	}
	
	
	
}
